package com.ctshk.app.bus.convertor;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ctshk.common.utils.Assist;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.common.utils.individual.conversion.*;
import com.ctshk.common.utils.individual.conversion.dto.*;
import com.ctshk.rpc.bus.dto.*;
import com.ctshk.rpc.bus.req.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *
 * </p>
 *
 * @author 彭柳霖
 * @since 2021/3/16
 */
public class Convertor {
    /**
     * 前端统一结构转成巴士产品结构，l2 to l1
     *
     * @param batchStrategyReq
     * @return
     */
    public static BusProductStrategySaveBatchReq frontToBusProduct(BatchStrategyReq batchStrategyReq) {
        BusProductStrategySaveBatchReq busProductStrategySaveBatchReq = new BusProductStrategySaveBatchReq();
        // 获取巴士产品ID列表
        busProductStrategySaveBatchReq.setBusProductIdList(batchStrategyReq.getProductIds());
        // 获取策略列表
        List<StrategyReq> batchStrategyReqStrategyList = batchStrategyReq.getStrategyList();
        List<BusProductStrategyReq> busProductStrategyReqList = new ArrayList<>();
        busProductStrategySaveBatchReq.setBusProductStrategyReqList(busProductStrategyReqList);
        if (batchStrategyReqStrategyList != null) {
        	for (StrategyReq strategyReq : batchStrategyReqStrategyList) {
        		BusProductStrategyReq busProductStrategyReq = new BusProductStrategyReq();
        		busProductStrategyReqList.add(busProductStrategyReq);
        		//策略主表
        		busProductStrategyReq.setIncreaseStrategyName(strategyReq.getName());
        		
        		//策略时间
        		List<ProductStrategyDateReq> productStrategyDateReqList = strategyReq.getTime();
        		List<BusProductStrategyDateReq> busProductStrategyDateReqList = EntityUtil.copyList(productStrategyDateReqList, BusProductStrategyDateReq.class);
        		
        		busProductStrategyReq.setBusProductStrategyDateReqList(busProductStrategyDateReqList);
        		//渠道加幅规则
        		ChannelSaleReq channelSaleReq = strategyReq.getChannelSaleReq();
        		
        		//渠道加幅规则-线下自营渠道
        		ProductOfflineChannelReq productOfflineChannelReq = channelSaleReq.getProductOfflineChannelReq();
        		if(!Objects.isNull(productOfflineChannelReq)){
        			busProductStrategyReq.setSelfSalePermissionType(productOfflineChannelReq.getPermissionType());
        			if (busProductStrategyReq.getSelfSalePermissionType() != null) {
        				if (busProductStrategyReq.getSelfSalePermissionType() == 2) {
        					BusProductIncreaseStrategyRuleReq busProductIncreaseStrategyRule = EntityUtil.copy(productOfflineChannelReq, BusProductIncreaseStrategyRuleReq.class);
        					busProductStrategyReq.setBusProductIncreaseStrategySelfRule(busProductIncreaseStrategyRule);
        				}
        				if (busProductStrategyReq.getSelfSalePermissionType() == 3) {
        					List<BusProductChannelOfflineSelfCustomReq> busProductChannelOfflineSelfCustomReqList = new ArrayList<>();
        					
        					List<ProductChannelOfflineSelfCustomReq> productChannelOfflineSelfCustomReqList = productOfflineChannelReq.getProductChannelOfflineSelfCustomReqList();
        					if (Assist.isNotEmpty(productChannelOfflineSelfCustomReqList)) {
        						for (ProductChannelOfflineSelfCustomReq productChannelOfflineSelfCustomReq : productChannelOfflineSelfCustomReqList) {
        							BusProductChannelOfflineSelfCustomReq busProductChannelOfflineSelfCustomReq = new BusProductChannelOfflineSelfCustomReq();
        							busProductChannelOfflineSelfCustomReq.setName(productChannelOfflineSelfCustomReq.getName());
        							busProductChannelOfflineSelfCustomReq.setCheckType(productChannelOfflineSelfCustomReq.getCheckType());
        							busProductChannelOfflineSelfCustomReq.setRelationId(productChannelOfflineSelfCustomReq.getRelationId());
        							
        							BusProductIncreaseStrategyRuleReq busProductIncreaseStrategyRule = EntityUtil.copy(productChannelOfflineSelfCustomReq, BusProductIncreaseStrategyRuleReq.class);
        							busProductChannelOfflineSelfCustomReq.setBusProductIncreaseStrategyRuleReq(busProductIncreaseStrategyRule);
        							busProductChannelOfflineSelfCustomReqList.add(busProductChannelOfflineSelfCustomReq);
        						}
        					}
        					busProductStrategyReq.setBusProductChannelOfflineSelfCustomReqList(busProductChannelOfflineSelfCustomReqList);
        				}
        			}
        		}
        		
        		
        		//渠道加幅规则-线下代理商渠道
        		ProductAgentReq productAgentReq = channelSaleReq.getProductAgentReq();
        		if(!Objects.isNull(productAgentReq)){
        			busProductStrategyReq.setAgentSalePermissionType(productAgentReq.getPermissionType());
        			if (busProductStrategyReq.getAgentSalePermissionType() != null) {
        				if (busProductStrategyReq.getAgentSalePermissionType() == 2) {
        					BusProductIncreaseStrategyRuleReq busProductIncreaseStrategyRule = EntityUtil.copy(productAgentReq, BusProductIncreaseStrategyRuleReq.class);
        					busProductStrategyReq.setBusProductIncreaseStrategyAgentRule(busProductIncreaseStrategyRule);
        				}
        				if (busProductStrategyReq.getAgentSalePermissionType() == 3) {
        					List<BusProductChannelOfflineAgentCustomReq> busProductChannelOfflineAgentCustomReqs = new ArrayList<>();
        					
        					List<ProductChannelOfflineAgentCustomReq> productChannelOfflineAgentCustomReqList = productAgentReq.getProductChannelOfflineAgentCustomReqList();
        					for (ProductChannelOfflineAgentCustomReq productChannelOfflineAgentCustomReq : productChannelOfflineAgentCustomReqList) {
        						BusProductChannelOfflineAgentCustomReq busProductChannelOfflineAgentCustomReq = new BusProductChannelOfflineAgentCustomReq();
        						busProductChannelOfflineAgentCustomReq.setAgentId(productChannelOfflineAgentCustomReq.getAgentId());
        						busProductChannelOfflineAgentCustomReq.setAgentName(productChannelOfflineAgentCustomReq.getAgentName());
        						
        						BusProductIncreaseStrategyRuleReq busProductIncreaseStrategyRule = EntityUtil.copy(productChannelOfflineAgentCustomReq, BusProductIncreaseStrategyRuleReq.class);
        						busProductChannelOfflineAgentCustomReq.setBusProductIncreaseStrategyRuleReq(busProductIncreaseStrategyRule);
        						busProductChannelOfflineAgentCustomReqs.add(busProductChannelOfflineAgentCustomReq);
        						
        					}
        					busProductStrategyReq.setBusProductChannelOfflineAgentCustomReqList(busProductChannelOfflineAgentCustomReqs);
        				}
        			}
        		}
        		
        		
        		//渠道加幅规则-线上自营渠道官方平台
        		List<ProductChannelOnlineSelfPlatformReq> productChannelOnlineSelfPlatformReqList = channelSaleReq.getProductChannelOnlineSelfPlatformReqList();
        		if(!CollectionUtils.isEmpty(productChannelOnlineSelfPlatformReqList)){
        			List<BusProductChannelOnlineSelfPlatformReq> busProductChannelOnlineSelfPlatformReqList = new ArrayList<>();
        			BusProductChannelOnlineSelfPlatformReq busProductChannelOnlineSelfPlatformReq = null;
        			for (ProductChannelOnlineSelfPlatformReq productChannelOnlineSelfPlatformReq : productChannelOnlineSelfPlatformReqList) {
        				busProductChannelOnlineSelfPlatformReq = new BusProductChannelOnlineSelfPlatformReq();
        				//渠道信息
        				busProductChannelOnlineSelfPlatformReq.setChannelId(productChannelOnlineSelfPlatformReq.getChannelId());
        				busProductChannelOnlineSelfPlatformReq.setChannelName(productChannelOnlineSelfPlatformReq.getChannelName());
        				
        				BusProductIncreaseStrategyRuleReq busProductIncreaseStrategyRule = EntityUtil.copy(productChannelOnlineSelfPlatformReq, BusProductIncreaseStrategyRuleReq.class);
        				busProductChannelOnlineSelfPlatformReq.setBusProductIncreaseStrategyRuleReq(busProductIncreaseStrategyRule);
        				busProductChannelOnlineSelfPlatformReqList.add(busProductChannelOnlineSelfPlatformReq);
        			}
        			busProductStrategyReq.setBusProductChannelOnlineSelfPlatformReqList(busProductChannelOnlineSelfPlatformReqList);
        		}
        		
        		
        		//渠道加幅规则-线上第三方平台
        		List<ProductChannelOnlineThirdPlatformReq> productChannelOnlineThirdPlatformReqList = channelSaleReq.getProductChannelOnlineThirdPlatformReqList();
        		if(!CollectionUtils.isEmpty(productChannelOnlineThirdPlatformReqList)){
        			List<BusProductChannelOnlineThirdPlatformReq> busProductChannelOnlineThirdPlatformReqList = new ArrayList<>();
        			BusProductChannelOnlineThirdPlatformReq busProductChannelOnlineThirdPlatformReq = null;
        			for (ProductChannelOnlineThirdPlatformReq productChannelOnlineThirdPlatformReq : productChannelOnlineThirdPlatformReqList) {
        				busProductChannelOnlineThirdPlatformReq = new BusProductChannelOnlineThirdPlatformReq();
        				//渠道信息
        				busProductChannelOnlineThirdPlatformReq.setPlatformId(productChannelOnlineThirdPlatformReq.getPlatformId());
        				busProductChannelOnlineThirdPlatformReq.setPlatformName(productChannelOnlineThirdPlatformReq.getPlatformName());
        				
        				BusProductIncreaseStrategyRuleReq busProductIncreaseStrategyRule = EntityUtil.copy(productChannelOnlineThirdPlatformReq, BusProductIncreaseStrategyRuleReq.class);
        				busProductChannelOnlineThirdPlatformReq.setBusProductIncreaseStrategyRuleReq(busProductIncreaseStrategyRule);
        				busProductChannelOnlineThirdPlatformReqList.add(busProductChannelOnlineThirdPlatformReq);
        			}
        			busProductStrategyReq.setBusProductChannelOnlineThirdPlatformReqList(busProductChannelOnlineThirdPlatformReqList);
        		}
        	}
        }
        return busProductStrategySaveBatchReq;
    }


    /**
     * 巴士产品结构转前端结构，l1 to l2
     *
     * @param busProductStrategyDTOList
     * @return
     */
    public static BatchStrategyDto busProductToFront(List<BusProductStrategyDTO> busProductStrategyDTOList){

        if(CollectionUtils.isEmpty(busProductStrategyDTOList)){
            return null;
        }
        BatchStrategyDto batchStrategyDto = new BatchStrategyDto();
        List<Long> productIds = new ArrayList<>();
        productIds.add(busProductStrategyDTOList.get(0).getBusProductId());

        List<StrategyDto> strategyList = new ArrayList<>();
        for(BusProductStrategyDTO busProductStrategyDTO : busProductStrategyDTOList){
            StrategyDto strategyDto = new StrategyDto();
            strategyDto.setName(busProductStrategyDTO.getIncreaseStrategyName());
            List<ProductStrategyDateReq> productStrategyDateReqList = EntityUtil.copyList(busProductStrategyDTO.getBusProductStrategyDateDTOList(), ProductStrategyDateReq.class);
            strategyDto.setTime(productStrategyDateReqList);
            ChannelSaleDto channelSaleDto = new ChannelSaleDto();

            //线下代理
            ProductAgentDto productAgentDto = new  ProductAgentDto();
            List<ProductChannelOfflineAgentCustomDto> productChannelOfflineAgentCustomDtoList = new ArrayList<>();
            if(!Objects.isNull(busProductStrategyDTO.getBusIncreaseStrategyAgentRule())){
                productAgentDto = EntityUtil.copy(busProductStrategyDTO.getBusIncreaseStrategyAgentRule(),ProductAgentDto.class);
            }
            List<BusProductChannelOfflineAgentCustomDTO> busProductChannelOfflineAgentCustomDTOList = busProductStrategyDTO.getBusProductChannelOfflineAgentCustomDTOList();
            if(!CollectionUtils.isEmpty(busProductChannelOfflineAgentCustomDTOList)){
                for(BusProductChannelOfflineAgentCustomDTO busProductChannelOfflineAgentCustomDTO : busProductChannelOfflineAgentCustomDTOList){
                    ProductChannelOfflineAgentCustomDto offlineAgentCustomDto = new ProductChannelOfflineAgentCustomDto();

                    BusProductIncreaseStrategyRuleDTO busProductIncreaseStrategyRuleDTO = busProductChannelOfflineAgentCustomDTO.getBusProductIncreaseStrategyRuleDTO();
                    offlineAgentCustomDto = EntityUtil.copy(busProductIncreaseStrategyRuleDTO,ProductChannelOfflineAgentCustomDto.class);
                    offlineAgentCustomDto.setAgentId(busProductChannelOfflineAgentCustomDTO.getAgentId());
                    offlineAgentCustomDto.setAgentName(busProductChannelOfflineAgentCustomDTO.getAgentName());
                    productChannelOfflineAgentCustomDtoList.add(offlineAgentCustomDto);
                }
                productAgentDto.setProductChannelOfflineAgentCustomReqList(productChannelOfflineAgentCustomDtoList);
            }
            productAgentDto.setPermissionType(busProductStrategyDTO.getAgentSalePermissionType());
            channelSaleDto.setProductAgentReq(productAgentDto);

            //线下自营
            ProductOfflineChannelDto productOfflineChannelDto = new ProductOfflineChannelDto();
            List<ProductChannelOfflineSelfCustomDto> productChannelOfflineSelfCustomDtoList = new ArrayList<>();
            if(!Objects.isNull(busProductStrategyDTO.getBusIncreaseStrategySelfRule())){
                productOfflineChannelDto = EntityUtil.copy(busProductStrategyDTO.getBusIncreaseStrategySelfRule(),ProductOfflineChannelDto.class);
            }
            List<BusProductChannelOfflineSelfCustomDTO> busProductChannelOfflineSelfCustomDTOList = busProductStrategyDTO.getBusProductChannelOfflineSelfCustomDTOList();
            if(!CollectionUtils.isEmpty(busProductChannelOfflineSelfCustomDTOList)){
                for(BusProductChannelOfflineSelfCustomDTO busProductChannelOfflineSelfCustomDTO : busProductChannelOfflineSelfCustomDTOList){
                    ProductChannelOfflineSelfCustomDto offlineSelfCustomDto = new ProductChannelOfflineSelfCustomDto();
                    BusProductIncreaseStrategyRuleDTO busProductIncreaseStrategyRuleDTO = busProductChannelOfflineSelfCustomDTO.getBusProductIncreaseStrategyRuleDTO();
                    offlineSelfCustomDto = EntityUtil.copy(busProductIncreaseStrategyRuleDTO,ProductChannelOfflineSelfCustomDto.class);
                    offlineSelfCustomDto.setRelationId(busProductChannelOfflineSelfCustomDTO.getRelationId());
                    offlineSelfCustomDto.setName(busProductChannelOfflineSelfCustomDTO.getName());
                    productChannelOfflineSelfCustomDtoList.add(offlineSelfCustomDto);
                }
                productOfflineChannelDto.setProductChannelOfflineSelfCustomReqList(productChannelOfflineSelfCustomDtoList);
            }
            productOfflineChannelDto.setPermissionType(busProductStrategyDTO.getSelfSalePermissionType());
            channelSaleDto.setProductOfflineChannelReq(productOfflineChannelDto);

            //线上自营
            List<ProductChannelOnlineSelfPlatformDto> productChannelOnlineSelfPlatformDtoList = new ArrayList<>();
            List<BusProductChannelOnlineSelfPlatformDTO> busProductChannelOnlineSelfPlatformDTOList = busProductStrategyDTO.getBusProductChannelOnlineSelfPlatformDTOList();
            if(!CollectionUtils.isEmpty(busProductChannelOnlineSelfPlatformDTOList)){
                for(BusProductChannelOnlineSelfPlatformDTO busProductChannelOnlineSelfPlatformDTO : busProductChannelOnlineSelfPlatformDTOList){
                    ProductChannelOnlineSelfPlatformDto productChannelOnlineSelfPlatformDto = new ProductChannelOnlineSelfPlatformDto();
                    BusProductIncreaseStrategyRuleDTO busProductIncreaseStrategyRuleDTO = busProductChannelOnlineSelfPlatformDTO.getBusProductIncreaseStrategyRuleDTO();
                    productChannelOnlineSelfPlatformDto = EntityUtil.copy(busProductIncreaseStrategyRuleDTO,ProductChannelOnlineSelfPlatformDto.class);
                    productChannelOnlineSelfPlatformDto.setChannelId(busProductChannelOnlineSelfPlatformDTO.getChannelId());
                    productChannelOnlineSelfPlatformDto.setChannelName(busProductChannelOnlineSelfPlatformDTO.getChannelName());
                    productChannelOnlineSelfPlatformDtoList.add(productChannelOnlineSelfPlatformDto);
                }
                channelSaleDto.setProductChannelOnlineSelfPlatformReqList(productChannelOnlineSelfPlatformDtoList);
            }

            //线上第三方
            List<ProductChannelOnlineThirdPlatformDto> productChannelOnlineThirdPlatformDtoList = new ArrayList<>();
            List<BusProductChannelOnlineThirdPlatformDTO> busProductChannelOnlineThirdPlatformDTOList = busProductStrategyDTO.getBusProductChannelOnlineThirdPlatformDTOList();
            if(!CollectionUtils.isEmpty(busProductChannelOnlineThirdPlatformDTOList)){
                for(BusProductChannelOnlineThirdPlatformDTO busProductChannelOnlineThirdPlatformDTO : busProductChannelOnlineThirdPlatformDTOList){
                    ProductChannelOnlineThirdPlatformDto productChannelOnlineThirdPlatformDto = new ProductChannelOnlineThirdPlatformDto();
                    BusProductIncreaseStrategyRuleDTO busProductIncreaseStrategyRuleDTO = busProductChannelOnlineThirdPlatformDTO.getBusProductIncreaseStrategyRuleDTO();
                    productChannelOnlineThirdPlatformDto = EntityUtil.copy(busProductIncreaseStrategyRuleDTO,ProductChannelOnlineThirdPlatformDto.class);
                    productChannelOnlineThirdPlatformDto.setPlatformId(busProductChannelOnlineThirdPlatformDTO.getPlatformId());
                    productChannelOnlineThirdPlatformDto.setPlatformName(busProductChannelOnlineThirdPlatformDTO.getPlatformName());
                    productChannelOnlineThirdPlatformDtoList.add(productChannelOnlineThirdPlatformDto);
                }
                channelSaleDto.setProductChannelOnlineThirdPlatformReqList(productChannelOnlineThirdPlatformDtoList);
            }
            strategyDto.setChannelSaleReq(channelSaleDto);
            strategyList.add(strategyDto);

        }
        batchStrategyDto.setProductIds(productIds);
        batchStrategyDto.setStrategyList(strategyList);
        return batchStrategyDto;

    }

}
